package server.core;

import javax.swing.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Enumeration;
import java.util.Hashtable;

public class ImageProxyTestDrive {
	ImageCompoent imageComponent;
	JFrame frame = new JFrame("CD Cover Viewer");
	JMenuBar menuBar;
	JMenu menu;
	Hashtable cds = new Hashtable();

	public static void main(String[] args) {
		try {
			ImageProxyTestDrive testDrive = new ImageProxyTestDrive();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public ImageProxyTestDrive() throws Exception {
		cds.put("A",
				"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1515349152281&di=51fb0c54e13e300027403618afd01a3c&imgtype=0&src=http%3A%2F%2Fa.hiphotos.baidu.com%2Fimage%2Fpic%2Fitem%2F6159252dd42a283474cb0ab251b5c9ea15cebf1d.jpg");
		cds.put("B",
		"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1515349152281&di=83b57255acebd2b583ef086cdfbca0ec&imgtype=0&src=http%3A%2F%2Fh.hiphotos.baidu.com%2Fimage%2Fpic%2Fitem%2Fdcc451da81cb39db11717befda160924aa1830d1.jpg");
		cds.put("C",
				"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1515349152281&di=1f8260d0a56fa608ba88ab77709450f9&imgtype=0&src=http%3A%2F%2Fe.hiphotos.baidu.com%2Fimage%2Fpic%2Fitem%2F8cb1cb1349540923a1694dc79858d109b3de4902.jpg");
		cds.put("D",
				"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1515349152281&di=41d022baa80fb215a69ea29c408ff661&imgtype=0&src=http%3A%2F%2Fh.hiphotos.baidu.com%2Fimage%2Fpic%2Fitem%2Faec379310a55b3190ceabdcc49a98226cffc1773.jpg");

		URL initialURL = new URL((String) cds.get("C"));
		menuBar = new JMenuBar();
		menu = new JMenu("Favorite CDs");
		menuBar.add(menu);
		frame.setJMenuBar(menuBar);
		for (Enumeration e = cds.keys(); e.hasMoreElements();) {
			String name = (String) e.nextElement();
			JMenuItem menuItem = new JMenuItem(name);
			menu.add(menuItem);
			menuItem.addActionListener(e1 -> {
				imageComponent.setIcon(new ImageProxy(getCDUrl(e1
						.getActionCommand())));
				frame.repaint();
			});

		}
		Icon icon = new ImageProxy(initialURL);
		imageComponent = new ImageCompoent(icon);
		frame.getContentPane().add(imageComponent);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(800, 600);
		frame.setVisible(true);
	}

	protected URL getCDUrl(String name) {
		try {
			return new URL((String) cds.get(name));
		} catch (MalformedURLException e) {
			e.printStackTrace();
			return null;
		}
	}
}
